import unittest
import tempfile
import shutil
import random
from fsmanager import *

class TestFsManager(unittest.TestCase):
    
    def test_1_timestamp_conversion(self):
        '''testing timestamp/date/time conversion functions'''
        ts_now = time.time()
        date_now = timestamp_to_string(ts_now)
        ts_test = timestamp_from_string(date_now)
        self.assert_(ts_now == ts_test, 
            'timestamp->datetime string->timestamp conversion failed')
        fn = '01-01-2009_23-59-59-123_120.00_[camera1].ogg'
        test_filename = os.path.join('tmp', fn)
        s = parse_filename(test_filename)
        self.assert_(s.day == 1 and s.month == 1 and s.year == 2009 and
            s.hours == 23 and s.minutes == 59 and s.seconds == 59 and
            s.microseconds == 123 and s.duration == 120.0 and 
            s.name == 'camera1' and s.extension == 'ogg',
            'parsing filename into structure failed')
    
    def test_2_dirsize(self):
        '''testing directory size function'''
        file_count = random.randint(5,10)
        file_size = random.randint(100, 1000)
        tmp_dir = tempfile.mkdtemp()
        
        for i in range(file_count):
            path = os.path.join(tmp_dir, 'testfile%d' % i)
            with open(path, 'w+') as f:
                for b in range(file_size): f.write(chr(0xff))
        
        count=0
        for (path, dirs, files) in os.walk(tmp_dir): 
            count += len(files)
        self.assert_(count == file_count,
            'failed to create %d files in %s for testing purposes' %
            (file_count, tmp_dir))
            
        test_size = file_count*file_size
        self.assert_(dirsize(tmp_dir) == test_size,
            'dirsize() reported an incorrect size')
        shutil.rmtree(tmp_dir)
    
    def test_3_freespace(self):
        '''testing free space checking function'''
        quota_over = 10 * 1000 + 1
        quota_under = 10 * 1000 - 1
        quota_even = 10 * 1000
        file_count = 10
        file_size = 1000
        tmp_dir = tempfile.mkdtemp()
        
        for i in range(file_count):
            path = os.path.join(tmp_dir, 'testfile%d' % i)
            with open(path, 'w+') as f:
                for b in range(file_size): f.write(chr(0xff))
                
        self.assert_(filecount(tmp_dir) == file_count,
            'failed to create %d files in %s for testing purposes' %
            (file_count, tmp_dir))
        
        test_free = (quota_over - (file_count * file_size), 
            file_count * file_size, quota_over)
        self.assert_(freespace(tmp_dir, quota_over) == test_free,
            'freespace() returned wrong value in "size under quota" test')
        
        test_free = (quota_under - (file_count * file_size), 
            file_count * file_size, quota_under)
        self.assert_(freespace(tmp_dir, quota_under) == test_free,
            'freespace() returned wrong value in "size over quota" test')
        
        test_free = (quota_even - (file_count * file_size), 
            file_count * file_size, quota_even)
        self.assert_(freespace(tmp_dir, quota_even) == test_free,
            'freespace() returned wrong value in "size equal to quota" test')
        
        shutil.rmtree(tmp_dir)
        
    def test_4_oldest_file(self):
        '''testing oldest file finder function (filename time)'''
        file_count = 10
        file_size = 1000
        tmp_dir = tempfile.mkdtemp()
        test_oldest = None
        
        for i in range(file_count):
            ts_str = timestamp_to_string(time.time())
            path = os.path.join(tmp_dir, '%s_1000.tst' % ts_str)
            if i == 0: test_oldest = timestamp_from_string(ts_str), path
            with open(path, 'w+') as f:
                for b in range(file_size): f.write(chr(0xff))
            # make some difference in timestamps
            wait_time = random.randint(1,50) / 100.0
            time.sleep(wait_time)
        
        self.assert_(filecount(tmp_dir) == file_count,
            'failed to create %d files in %s for testing purposes' %
            (file_count, tmp_dir))
        
        result_oldest = oldestfile(tmp_dir)
        self.assert_(test_oldest == result_oldest,
            'oldest file test failed, unexpected oldest')
        
        shutil.rmtree(tmp_dir)

    def test_5_oldest_file_fs(self):
        '''testing oldest file finder function (filesystem time)'''
        file_count = 10
        file_size = 1000
        tmp_dir = tempfile.mkdtemp()
        test_oldest = None

        for i in range(file_count):
            ts_str = timestamp_to_string(time.time())
            path = os.path.join(tmp_dir, '%s_1000.tst' % ts_str)
            if i == 0: test_oldest = timestamp_from_string(ts_str), path
            with open(path, 'w+') as f:
                for b in range(file_size): f.write(chr(0xff))
            # make some difference in timestamps
            wait_time = random.randint(1,50) / 100.0
            time.sleep(wait_time)
        
        count=0
        for (path, dirs, files) in os.walk(tmp_dir): 
            count += len(files)
        self.assert_(count == file_count,
            'failed to create %d files in %s for testing purposes' %
            (file_count, tmp_dir))
        
        result_oldest = oldestfile(tmp_dir, True)
        result_oldest = (round(result_oldest[0],1), result_oldest[1])
        test_oldest = (round(test_oldest[0],1), test_oldest[1])
        self.assert_(test_oldest == result_oldest,
            'oldest file test failed, unexpected oldest')
        
        shutil.rmtree(tmp_dir)
        
        
def run_tests():
    suite = unittest.TestLoader().loadTestsFromTestCase(TestFsManager)
    unittest.TextTestRunner(verbosity=2).run(suite)
